1339C - Powered Addition - CodeForces Solution


bitmasks brute force greedy *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
 
#define ll long long
#define lli long long int
#define f(i,n) for(int i=0;i<n;i++)
#define ri(i,n) for(int i=n-1;i>=0;i--)
#define fl(i,m,n) for(int i=m;i<=n;i++)
#define rl(i,m,n) for(int i=n;i>=m;i--)
#define pb  push_back
#define py cout<<"YES"
#define pn cout<<"NO"
 
const ll mod=1e9+7;
typedef vector<ll> vll;
 
void output(vector<int>arr, int n)
{
    f(i,n)
        std::cout << arr[i]<<" ";
    cout<< std::endl;
}
 
lli max(lli a, lli b)
{
    return a>b?a:b;
}
lli min(lli a, lli b)
{
    return a<b?a:b;
}
lli gcd(long long int a, long long int b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
lli lcm(lli a,lli b){return (a/gcd(a,b)*b);}
 
int main() {
	// your code goes here
	int T;
	std::cin >> T;
	for(int ab=1;ab<=T;ab++)
	{
	    //------------------------------------------------------------------------

	    lli n;
	    std::cin >> n;
	    lli arr[n];
	    f(i,n)
	    {
	        std::cin >> arr[i];
	    }
	    if(n==1)
	    {
	        std::cout << 0 << std::endl;
	        continue;
	    }
	    lli maxi=arr[0],df=0;
	    f(i,n)
	    {
	        if(arr[i]>maxi)
	            maxi=arr[i];
	        else
	            df=max(df,maxi-arr[i]);
	    }
	    if(df==0)
	    {
	        std::cout << 0 << std::endl;
	        continue;
	    }
	    int ans=log2(df);
	    std::cout << ans+1 << std::endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent